﻿Shader "Effects/ShockWave" 
{

    Properties {
        _Speed ("Speed", float) = 1
        _Smooth ("Smooth", float) = 5
        _Color ("Main Color", Color) = (1,1,1,1)
        _MainTex ("Base (RGB) Transparency (A)", 2D) = "white" {}
    }

	SubShader {
        Tags { "Queue" = "Transparent" }

		// Grab the screen behind the object into _GrabTexture
        // GrabPass { }

		Pass
		{
            Blend One One

			CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

			uniform sampler2D _MainTex;
			uniform sampler2D _GrabTexture;

			struct v2f {
				float4 pos : SV_POSITION;
				float4 uvCoord : TEXCOORD0;
			};
      
			float _Speed;
			float _Smooth;

			v2f vert (appdata_base v)
			{
				v2f o;
				o.uvCoord = v.texcoord;
				o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
				return o;
			}

			fixed4 frag (v2f i) : COLOR0 {
				float2 diff = i.uvCoord.xy - 0.5;
				float distance = length(diff) * 2;
				float deformation = abs(distance - 1);
				deformation = max(deformation * _Smooth, 0);


				if (distance >= 1) {
					return fixed4(0, 0, 0, 0);
				} else {
					//  return tex2D(_MainTex, i.uvCoord);
					 return fixed4(1.0 - deformation, 0, 0, 1.0 - deformation);	
				}
			}
			ENDCG
		}
	}

	SubShader {
      Pass {   
         CGPROGRAM
 
         #pragma vertex vert  
         #pragma fragment frag 
 
         #include "UnityCG.cginc"
 
         // User-specified uniforms
         uniform samplerCUBE _Cube;   
 
         struct vertexInput {
            float4 vertex : POSITION;
            float3 normal : NORMAL;
         };
         struct vertexOutput {
            float4 pos : SV_POSITION;
            float3 normalDir : TEXCOORD0;
            float3 viewDir : TEXCOORD1;
         };
 
         vertexOutput vert(vertexInput input) 
         {
            vertexOutput output;
 
            float4x4 modelMatrix = _Object2World;
            float4x4 modelMatrixInverse = _World2Object; 
               // multiplication with unity_Scale.w is unnecessary 
               // because we normalize transformed vectors
 
			float4 bif = mul(modelMatrix, input.vertex) - float4(_WorldSpaceCameraPos, 1.0);
            output.viewDir = bif;
            output.normalDir = normalize(mul(float4(input.normal, 0.0), modelMatrixInverse));
            output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
            return output;
         }
 
         float4 frag(vertexOutput input) : COLOR
         {
            float refractiveIndex = 1.5;
            float3 refractedDir = refract(normalize(input.viewDir), 
               normalize(input.normalDir), 1.0 / refractiveIndex);
            return texCUBE(_Cube, refractedDir);
         }
 
         ENDCG
      }
   }
	Fallback Off
}
